<template>
  <!-- filter -->
  <view class="filter-box">
    <uni-datetime-picker
      v-model="range"
      type="daterange"
      :border="false"
      :clear-icon="false"
      rangeSeparator="至"
      @change="change"
    />
  </view>
  <!-- list -->
  <view class="totalAll" :style="{ height: outerHeight + 'px' }">
    <view class="totalAlls" @click="open()">
      <text>{{ SceneName }}</text>
      <image src="../../static/account/account-arrow-down.png" mode=""></image>
    </view>
    <scroll-view :scroll-y="true" class="list-box" @scrolltolower="loadMore" :style="{ height: pageHeight + 'px' }">
      <view v-if="BillList.length < 1" class="flex-col-hcenter" style="padding-top: 60rpx">
        <image :src="Empty" style="width: 352rpx; height: 352rpx"></image>
        <text class="tip">暂无使用记录</text>
      </view>
      <view class="list-item" v-for="(item, index) in BillList" :key="index">
        <view class="flex-jcsb" style="font-size: 30rpx">
          <view class="text">
            <text>{{ item.memo }}</text>
            <text v-if="item.money_type">{{ item.money_type }}</text>
          </view>
          <view :class="item.type + '-text'" style="font-weight: bold">{{ item.money }}</view>
        </view>
        <view class="flex-jcsb" style="margin-top: 10rpx; font-size: 26rpx">
          <view style="color: #666">{{ item.createtime }}</view>
          <view>{{ item.driver_name }}</view>
        </view>
      </view>
    </scroll-view>
  </view>
  <!-- 筛选弹框 -->
  <uni-popup ref="FilterPopup" type="bottom" :is-mask-click="false">
    <view class="Filter">
      <view class="filter_text">
        <uni-icons type="closeempty" size="26" @click="close()"></uni-icons>
        <text>选择筛选项</text>
        <text></text>
      </view>
      <scroll-view :scroll-y="true" style="height: 35vh;">
        <view class="filter_list">
          <text
            v-for="(item, index) in SceneList"
            :key="index"
            @click="changeAccount(item.name, item.scene)"
            :style="{
              background: SceneName == item.name ? '#1B91FF ' : '',
              color: SceneName == item.name ? '#fff' : '',
            }"
          >
            {{ item.name }}
          </text>
        </view>
      </scroll-view>
    </view>
  </uni-popup>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { format, subDays } from 'date-fns';
import { isH5 } from '@/utils/platform';
import { screenLists, CapitalDetails } from '@/gql/user';
import { Empty } from '@/utils/Static';
import { getDriverID } from '@/stores/driverID';
import { toPublish } from '@mqtt';
const driver_id = getDriverID();
// data
const pageHeight = ref('');
const outerHeight = ref('');

const FilterPopup = ref(null);
const SceneList = ref([]);
const SceneName = ref('');
const scene = ref('');

let now = new Date();
let end = format(now, 'yyyy-MM-dd');
let daysAgo = subDays(now, 6);
let start = format(daysAgo, 'yyyy-MM-dd');
const range = ref([start, end]);

const BillList = ref([]);
const page = ref(1);
const lastpage = ref(1);
const currentPage = ref(1);
const isLoading = ref(true);
const props = defineProps({
  type: {
    type: String,
    default: '',
  },
});
onMounted(() => {
  const num = isH5 ? 110 : 150;
  uni.getSystemInfo({
    success: function(res) {
      outerHeight.value = (res.windowHeight - num).toString();
	  pageHeight.value = (outerHeight.value - 50).toString();
    },
  });
});
function screenList() {
  BillList.value = [];
  page.value = 1;
  lastpage.value = 1;
  currentPage.value = 1;
  scene.value = '';
  const payload = {
    query: screenLists,
    variables: { type: props.type },
  };
  toPublish('ql/driver/sceneList', payload, (obj: any) => {
    SceneList.value = obj.data.scene_list;
    SceneName.value = obj.data.scene_list[0].name;
    scene.value = obj.data.scene_list[0].scene;
    init();
  });
}

function init() {
  isLoading.value = true;
  const payload = {
    query: CapitalDetails,
    variables: {
      page: currentPage.value,
      start,
      end,
      driver_id,
      type: props.type,
      scene: scene.value,
    },
  };
  toPublish('ql/driver/moneylogList', payload, (obj: any) => {
    const { moneylog_list } = obj.data;
    isLoading.value = false;
    BillList.value = [...BillList.value, ...moneylog_list.list];
    lastpage.value = moneylog_list.lastpage;
  });
}
/**
 * 加载更多
 */
function loadMore() {
  if (currentPage.value < lastpage.value) {
    currentPage.value++;
    init();
  }
}

/**
 * 时间筛选
 */
function change([sta, en]) {
  currentPage.value = 1;
  BillList.value = [];
  start = sta;
  end = en;
  init();
}
/**
 * @param {Object} name 筛选项名称
 * @param {Object} sce  筛选项编号
 */
function changeAccount(name, sce) {
  close();
  SceneName.value = name;
  scene.value = sce;
  currentPage.value = 1;
  BillList.value = [];
  init();
}
function open() {
  FilterPopup.value.open();
}
function close() {
  FilterPopup.value.close();
}
defineExpose({
  screenList,
});
</script>

<style scoped lang="less">
@import url('../account/less/accountList.less');
</style>
<style>
.filter-box >>> .uni-date__x-input {
  font-size: 30rpx !important;
}
.filter-box >>> .uni-date-x {
  font-size: 30rpx !important;
  background-color: transparent !important;
}
.filter-box >>> .uni-icons {
  font-size: 48rpx !important;
}
</style>
